package com.haoan.index.service.impl;


import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoan.index.common.enums.ValiFlag;

import com.haoan.index.mapper.PostcardMapper;
import com.haoan.index.domain.Postcard;
import com.haoan.index.service.PostcardService;

/**
 * 明信片Service业务层处理
 *
 * @author autoGen
 * @date 2025-02-19
 */
@Service
public class PostcardServiceImpl extends ServiceImpl<PostcardMapper,Postcard> implements PostcardService
{
    /**
     * 查询明信片
     *
     * @param id 明信片主键
     * @return 明信片
     */
    @Override
    public Postcard selectPostcardById(Long id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询明信片列表
     *
     * @param postcard 明信片
     * @return 明信片
     */
    @Override
    public List<Postcard> selectPostcardList(Postcard postcard)
    {
        LambdaQueryWrapper<Postcard> queryWrapper = new LambdaQueryWrapper<>();
        //todo 添加查询条件
        queryWrapper.eq(postcard.getId() != null, Postcard::getId, postcard.getId());
        queryWrapper.eq(postcard.getDay() != null, Postcard::getDay, postcard.getDay());
        queryWrapper.eq(postcard.getPicture() != null, Postcard::getPicture, postcard.getPicture());
        queryWrapper.eq(postcard.getText() != null, Postcard::getText, postcard.getText());
        queryWrapper.eq(postcard.getAuthor() != null, Postcard::getAuthor, postcard.getAuthor());
        queryWrapper.eq(postcard.getCreateTime() != null, Postcard::getCreateTime, postcard.getCreateTime());
        queryWrapper.eq(postcard.getCreateBy() != null, Postcard::getCreateBy, postcard.getCreateBy());
        queryWrapper.eq(postcard.getUpdateTime() != null, Postcard::getUpdateTime, postcard.getUpdateTime());
        queryWrapper.eq(postcard.getUpdateBy() != null, Postcard::getUpdateBy, postcard.getUpdateBy());
        queryWrapper.eq(Postcard::getValiFlag,ValiFlag.VALID );
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增明信片
     *
     * @param postcard 明信片
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPostcard(Postcard postcard)
    {
        return baseMapper.insert(postcard);
    }

    /**
     * 修改明信片
     *
     * @param postcard 明信片
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePostcard(Postcard postcard)
    {
        return baseMapper.updateById(postcard);
    }

    /**
     * 批量删除明信片
     *
     * @param ids 需要删除的明信片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePostcardByIds(Long [] ids)
    {
        Postcard postcard = new Postcard();
        postcard.setValiFlag(ValiFlag.INVALID);
        LambdaQueryWrapper<Postcard> updQw = new LambdaQueryWrapper<>();
        updQw.in(Postcard::getId, ids);
        return baseMapper.update(postcard, updQw);
    }

    /**
     * 删除明信片信息
     *
     * @param id 明信片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(Long id)
    {
        Postcard postcard = getById(id);
        postcard.setValiFlag(ValiFlag.INVALID);
        return baseMapper.updateById(postcard);
    }
}
